using System;
using NUnit.Framework;

namespace ValidationFramework.Tests
{
    [TestFixture]
    public class LengthStringRuleAttributeTest
    {

        private const int expectedMaximum = 3;
        private const int expectedMinimum = 1;

        [Test]
        public void CheckRules()
        {
            var ruleAttribute = new LengthStringRuleAttribute(expectedMaximum);
            var propertyRule = ruleAttribute.CreateRule(new MockPropertyDescriptor<string>("foo"));
            var parameterRule = ruleAttribute.CreateRule(new MockParameterDescriptor<string>("foo"));
            Assert.IsNotNull(propertyRule);
            Assert.IsNotNull(parameterRule);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Minimum too low.\r\nParameter name: value")]
        public void MinimumTooSmall()
        {
            new LengthStringRuleAttribute(3) {Minimum = (-1)};
        }

        [Test]
        public void CheckValues()
        {
            var ruleAttribute = new LengthStringRuleAttribute(expectedMaximum)
                                                          {
                                                              Minimum = expectedMinimum,
                                                              TrimWhiteSpace = false
                                                          };
            Assert.IsFalse(ruleAttribute.TrimWhiteSpace);
            Assert.AreEqual(expectedMaximum, ruleAttribute.Maximum);
            Assert.AreEqual(expectedMinimum, ruleAttribute.Minimum);
            var propertyRule = (LengthStringRule)ruleAttribute.CreateRule(new MockPropertyDescriptor<string>("foo"));
            var parameterRule = (LengthStringRule)ruleAttribute.CreateRule(new MockParameterDescriptor<string>("foo"));

            Assert.AreEqual(expectedMaximum, propertyRule.Maximum);
            Assert.AreEqual(expectedMinimum, propertyRule.Minimum);
            Assert.IsFalse(propertyRule.TrimWhiteSpace);

            Assert.AreEqual(expectedMaximum, parameterRule.Maximum);
            Assert.AreEqual(expectedMinimum, parameterRule.Minimum);
            Assert.IsFalse(parameterRule.TrimWhiteSpace);


        }

        [Test]
        public void CallAttributeTester()
        {
            AttributeTester.CheckDefaultValues<string>(new LengthStringRuleAttribute(expectedMaximum), true, true);
            AttributeTester.CheckNonDefaultValues<string>(new LengthStringRuleAttribute(expectedMaximum), true, true);
        }

    }
}